// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
Shader "YoFi/UI/RTMergeNew_HairToning"
{
    Properties
    {
        [PerRendererData] _MainTex("Texture", 2D) = "white" {}

        [Header(Hair)]
        _MaskTex("Mask Texture", 2D) = "white" {}
        _WarmColor("暖色调", color) = (0.8,0.2,0.2,0.5)
        _CoolColor("冷色调", color) = (0.0,0.5,0.0,0.5)
        _OtherColor("TB 冷色调", color) = (0.0,0.5,0.0,0.5)
    }

    SubShader
    {
        // Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
        Tags 
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
        Cull Off
        ZWrite Off
        ZTest Always
        Blend SrcAlpha OneMinusSrcAlpha , One OneMinusSrcAlpha


        Pass
        {
            Name "Default"
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #pragma shader_feature _ART_EDITOR 
            #pragma target 2.0

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"


            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            sampler2D _MaskTex;
            #ifdef _ART_EDITOR
                float4 _WarmGColor, _CoolGColor,_OtherGColor;
                #define _WarmColor (_WarmGColor)
                #define _CoolColor (_CoolGColor)
                #define _OtherColor (_OtherGColor)
            #else
                float4 _WarmColor, _CoolColor,_OtherColor;
            #endif
            
            float3 DT_PS_OVERLAY(in float3 _layer0, in float3 _layer1)
            {
                const float3 layer0 = _layer0.rgb;// * _layer0.a;
                const float3 layer1 = _layer1.rgb;// * _layer1.a;
                // 预先计算 layer0 和 layer1 的最大值，减少重复计算
                const float layer0Max = max(layer0.r, max(layer0.g, layer0.b));
                // 计算步进值，判断 layer0 的最大值是否大于 0.5
                const float stepValue = step(0.5, layer0Max);
                float3 twoLayer0Layer1 = 2.0 * layer0 * layer1;
                float3 oneMinusLayer0 = 1.0 - layer0;
                float3 oneMinusLayer1 = 1.0 - layer1;
                float3 oneMinusTwoOneMinusLayer0OneMinusLayer1 = 1.0 - 2.0 * oneMinusLayer0 * oneMinusLayer1;
                // 使用 lerp 函数进行插值计算
                return lerp(twoLayer0Layer1, oneMinusTwoOneMinusLayer0OneMinusLayer1, stepValue);
            }

            inline float3 DT_PS_MULTIPLY(float3 layer0, float3 layer1) {
                return layer1 * layer0;
            }

            inline float3 DT_PS_SCREEN(float3 layer0, float3 layer1) {
                return layer0.rgb + layer1.rgb - layer0.rgb * layer1.rgb;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                // Sample the texture
                half4 mainTex = tex2D(_MainTex, i.uv);
                half4 mask = tex2D(_MaskTex, i.uv);
                mask.rgb *= mask.a;
                half4 result = half(0);

                float m0_l = (1.0 - mask.b); 
                float src0_l = mask.b;

                half3 highColor = mask.g;

                half3 color1 = DT_PS_OVERLAY( m0_l * mainTex.rgb,_WarmColor.rgb);
                half3 color2 = DT_PS_OVERLAY( m0_l * mainTex.rgb,_CoolColor.rgb);
                half3 color3 = src0_l * mainTex.rgb;
                half3 color4 = DT_PS_OVERLAY(src0_l * mainTex.rgb ,_OtherColor.rgb);

                half alpha1 = (mainTex.a);
                float3 mainCol = saturate(color1 + color3 + highColor);
                float3 darkCol = saturate(color2 + color4 + highColor);

                result.rgb = lerp(darkCol,mainCol,mask.r);// lerp(color1.rgb,color2.rgb,mask.b * 0);
                // result.rgb = _WarmColor.rgb;

                result.a = alpha1;

                return result;
            }
        ENDCG
        }
    }
}
